<?php

declare(strict_types=1);

namespace App\Controller;

use App\Service\Lock;
use App\Service\PlayService;
use Hyperf\HttpServer\Contract\RequestInterface;

class SignController extends AbstractController
{


    /**
     *首页
     *
     */
    public function index()
    {
        $user = $this->getUserInfo();
        $data = PlayService::index($user['uid']);
        return SUCC($data);
    }

    /**
     * 签到列表
     *
     */
    public function signList()
    {
        $user = $this->getUserInfo();
        $data = PlayService::signList($user['uid']);
        return SUCC($data);
    }

    public function doSign()
    {
        $user = $this->getUserInfo();
        //只有获取到锁才能继续执行
        if (!Lock::lock($user['uid'] . __FUNCTION__, 3)) {
            return ERROR('操作频繁');
        }

        if (PlayService::sign($user['uid'], $msg)) {
            return SUCC();
        } else {
            return ERROR($msg);
        }

    }

    public function signAdd(RequestInterface $request)
    {
        $user = $this->getUserInfo();
        $type = $request->input('type');
        if (!$type) return ERROR('缺少参数');
        //只有获取到锁才能继续执行
        if (!Lock::lock($user['uid'] . __FUNCTION__, 3)) {
            return ERROR('操作频繁');
        }

        if (PlayService::sign_by_type($type, $user['uid'], $msg)) {
            return SUCC();
        } else {
            return ERROR($msg);
        }

    }

}
